﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using Tao.FreeGlut;

namespace PacMan3D.Utils
{
    public class Shadow
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fLightPos"></param>
        /// <param name="fPlane"></param>
        /// <returns></returns>
        public static float[] BuildShadowMatrix(float[] fLightPos, float[] fPlane)
        { 
            float dotp;
            float[] tmpMatrix = new float[16] ;

            // Calculate the dot-product between the plane and the light's position   
            dotp = fPlane[0] * fLightPos[0] + 
                    fPlane[1] * fLightPos[1] + 
                    fPlane[1] * fLightPos[2] + 
                    fPlane[3] * fLightPos[3];

            // First column   
            tmpMatrix[0] = dotp - fLightPos[0] * fPlane[0];
            tmpMatrix[4] = 0.0f - fLightPos[0] * fPlane[1];
            tmpMatrix[8] = 0.0f - fLightPos[0] * fPlane[2];
            tmpMatrix[12] = 0.0f - fLightPos[0] * fPlane[3];   

            // Second column   
            tmpMatrix[1] = 0.0f - fLightPos[1] * fPlane[0];
            tmpMatrix[5] = dotp - fLightPos[1] * fPlane[1];
            tmpMatrix[9] = 0.0f - fLightPos[1] * fPlane[2];
            tmpMatrix[13] = 0.0f - fLightPos[1] * fPlane[3];   

            // Third column   
            tmpMatrix[2] = 0.0f - fLightPos[2] * fPlane[0];
            tmpMatrix[6] = 0.0f - fLightPos[2] * fPlane[1];
            tmpMatrix[10] = dotp - fLightPos[2] * fPlane[2];
            tmpMatrix[14] = 0.0f - fLightPos[2] * fPlane[3];   

            // Fourth column   
            tmpMatrix[3] = 0.0f - fLightPos[3] * fPlane[0];
            tmpMatrix[7] = 0.0f - fLightPos[3] * fPlane[1];
            tmpMatrix[11] = 0.0f - fLightPos[3] * fPlane[2];
            tmpMatrix[15] = dotp - fLightPos[3] * fPlane[3];

            return tmpMatrix;
        }

        /// <summary>
        /// This method find the plane equation given 3 points  
        /// </summary>
        /// <param name="v0"></param>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static float[] FindPlane(Point3D v0, Point3D v1, Point3D v2)
        {   
            Vector vec0 = new Vector();
            Vector vec1 = new Vector();
            float[] plane = new float[4];
            
            // Need 2 vectors to find cross product   
            vec0.X = v1.X - v0.X;   
            vec0.Y = v1.Y - v0.Y;   
            vec0.Z = v1.Z - v0.Z;   

            vec1.X = v2.X - v0.X;   
            vec1.Y = v2.Y - v0.Y;   
            vec1.Z = v2.Z - v0.Z;   

            // Find cross product to get A, B, and C of plane equation   
            plane[0] =   vec0.Y * vec1.Z - vec0.Z * vec1.Y;   
            plane[1] = -(vec0.X * vec1.Z - vec0.Z * vec1.X);   
            plane[2] =   vec0.X * vec1.Y - vec0.Y * vec1.X;   
            plane[3] = -(plane[0] * v0.X + plane[1] * v0.Y + plane[2] * v0.Z);
            return plane;
        }  
    }
}
